package com.thinkgem.dingdan.modules.bu.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.dingdan.common.config.Global;
import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.utils.StringUtils;
import com.thinkgem.dingdan.common.web.BaseController;
import com.thinkgem.dingdan.modules.bu.entity.BuSalesman;
import com.thinkgem.dingdan.modules.bu.service.BuSalesmanService;
import com.thinkgem.dingdan.modules.cms.entity.Site;
import com.thinkgem.dingdan.modules.sys.entity.Office;
import com.thinkgem.dingdan.modules.sys.entity.User;
import com.thinkgem.dingdan.modules.sys.utils.UserUtils;

/**
 * 客户Controller
 * @author 
 * @version 2018-09-15
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buSalesman")
public class BuSalesmanController extends BaseController {

	@Autowired
	private BuSalesmanService buSalesmanService;
	
	@ModelAttribute
	public BuSalesman get(@RequestParam(required=false) String id) {
		BuSalesman entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = buSalesmanService.get(id);
		}
		if (entity == null){
			entity = new BuSalesman();
		}
		return entity;
	}
	
	@RequiresPermissions("bu:buSalesman:view")
	@RequestMapping(value = {"list", ""})
	public String list(BuSalesman buSalesman, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BuSalesman> page = buSalesmanService.findPage(new Page<BuSalesman>(request, response), buSalesman); 
		model.addAttribute("page", page);
		return "modules/bu/buSalesmanList";
	}

	@RequiresPermissions("bu:buSalesman:edit")
	@RequestMapping(value = "form")
	public String form(BuSalesman buSalesman, Model model) {
		User user = UserUtils.getUser();
		model.addAttribute("customerId", user.getId());
		model.addAttribute("customerName", user.getName());
		model.addAttribute("buSalesman", buSalesman);
		return "modules/bu/buSalesmanForm";
	}
	
	@RequiresPermissions("bu:buSalesman:view")
	@RequestMapping(value = "view")
	public String view(HttpServletRequest request, HttpServletResponse response,BuSalesman buSalesman, Model model) {
		
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		model.addAttribute("buSalesman", buSalesman);
		return "modules/bu/buSalesmanView";
		
	}
	
	@RequiresPermissions("bu:buSalesman:toedit")
	@RequestMapping(value = "toUpdate")
	public String toUpdate(HttpServletRequest request,HttpServletResponse response,BuSalesman buSalesman, Model model) {
		
		User user = UserUtils.getUser();
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		model.addAttribute("customerId", user.getId());
		model.addAttribute("customerName", user.getName());
		model.addAttribute("buSalesman", buSalesman);
		return "modules/bu/buSalesmanEdit";
	}
	
	

	@RequiresPermissions("bu:buSalesman:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,HttpServletResponse response,BuSalesman buSalesman, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, buSalesman)){
			return form(buSalesman, model);
		}
		User user = UserUtils.getUser();
		Office office = user.getOffice();
		buSalesman.setOffice(office);
	    String siteId = Site.getCurrentSiteId();
	    Site site = new Site();
	    site.setId(siteId);
	    buSalesman.setSite(site);
		buSalesman.setCreateBy(user);
		buSalesman.setCreateDate(new Date());
		buSalesman.setDelFlag("0");
		buSalesmanService.save(buSalesman);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buSalesman/?repage";
	}
	
	@RequiresPermissions("bu:buSalesman:delete")
	@RequestMapping(value = "delete")
	public String delete(BuSalesman buSalesman, RedirectAttributes redirectAttributes) {
		
		buSalesmanService.delete(buSalesman);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/bu/buSalesman/?repage";
	}

}